<template>
  <div>
    <div class="content">
      <div v-for="(item, i) in data" :key="i" class="contentBox">
        <el-card shadow="always" :body-style="{ padding: '0px' }">
          <el-image
            :src="item.picUrl"
            fit="cover"
            @click="getItem(item.id)"
            lazy
          >
            <div slot="placeholder" class="image-slot">
              <el-image
                src="https://s4.music.126.net/style/web2/img/default/default_album.jpg"
                lazy
              ></el-image>
            </div>
          </el-image>
          <div style="padding: 14px">
            <el-tooltip
              class="item"
              effect="dark"
              :content="item.name"
              placement="top"
            >
              <div class="desc">
                {{ item.name }}
              </div>
            </el-tooltip>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  mounted() {
    this.id = window.sessionStorage.getItem('artists');
    this.getData();
  },
  computed: {
    ...mapState(['resArtists']),
  },
  watch: {
    resArtists(newVal, oldVal) {
      this.id = newVal.resArtists;
      this.getData();
    },
  },
  data() {
    return {
      id: null,
      data: null,
    };
  },
  methods: {
    ...mapActions(['saveResArtists']),
    async getData() {
      this.data = null;
      this.$api.simiArt({ id: parseInt(this.id), cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.data = ret.artists;
      });
    },

    async getItem(e) {
      window.sessionStorage.setItem('artists', e);
      await this.saveResArtists({ resArtists: e });
    },
  },
};
</script>

<style scoped>
.content {
  display: flex;
  flex-wrap: wrap;
}

.contentBox {
  margin-right: 5px;
}

.more {
  cursor: pointer;
  margin-top: 10px;
  color: #606266;
}

.el-card {
  margin-bottom: 10px;
  cursor: pointer;
  width: 200px;
  position: relative;
}

.el-card .el-image {
  width: 200px;
  height: 200px;
}

.desc {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
